package com.ctshk.rpc.hotel.APIUtils;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class CreateXMLFile {
    public static File createXMLFileFunc() throws IOException {
        File file = new File("ntstatic.xml");
        try {
            // 1、生成一个根节点
            Element rss = new Element("rss");
            // 2、为节点添加属性
            rss.setAttribute("version", "2.0");
            // 3、生成一个document对象
            Document document = new Document(rss);
            Element channel = new Element("channel");
            rss.addContent(channel);
            Element title = new Element("title");
            title.setText("测试");
            channel.addContent(title);
            Format format = Format.getCompactFormat();
            // 设置换行Tab或空格
            format.setIndent("	");
            format.setEncoding("UTF-8");
            // 4、创建XMLOutputter的对象
            XMLOutputter outputer = new XMLOutputter(format);
            // 5、利用outputer将document转换成xml文档
            //File file = new File("rssNew.xml");
            outputer.output(document, new FileOutputStream(file));
            System.out.println("生成ntstatic.xml成功");
        } catch (Exception e) {
            System.out.println("生成ntstatic.xml失败");
        }
        return file;
    }
}
